﻿using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Yuri.SolidWorks.MainFunction;

namespace Yuri.SolidWorks.Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        ModelDoc2 swModel;

        /// <summary>
        /// 检查当前打开的文件
        /// </summary>
        /// <returns></returns>
        private bool CheckswModel()
        {

            swModel = ConnectSwApp.swApp.ActiveDoc;

            if (swModel == null)
            {
                MessageBox.Show("请打开SolidWorks文件后重试！");
                return false;
            }

            return true;

        }


        /// <summary>
        /// 获取属性
        /// </summary>
        /// <param name="swModel">模型</param>
        /// <param name="ConfigName">配置名</param>
        /// <returns></returns>
        public static List<SwProperty> GetConfigProperty(ModelDoc2 swModel, string ConfigName = "")
        {

            List<SwProperty> swProperties = new List<SwProperty>();

            var cusPropMgr = swModel.Extension.CustomPropertyManager[ConfigName];

            object propNames = null;
            object propTypes = null;
            object propValues = null;
            object propResolved = null;

            var propCount = cusPropMgr.GetAll2(ref propNames, ref propTypes, ref propValues, ref propResolved);

            if (propCount > 0)
            {
                var propNamesList = (object[])propNames;
                var propTypesList = (int[])propTypes;
                var propValuesList = (string[])propValues;

                for (int i = 0; i < propCount; i++)
                {
                    var curProperty = new SwProperty();
                    curProperty.pType = (swCustomInfoType_e)propTypesList[i];
                    curProperty.pValue = propValuesList[i].ToString();
                    curProperty.pName = propNamesList[i].ToString();
                    swProperties.Add(curProperty);
                }
            }

            return swProperties;

        }

        /// <summary>
        /// 属性类
        /// </summary>
        public class SwProperty
        {
            /// <summary>
            /// 属性名称
            /// </summary>
            public string pName { get; set; }

            /// <summary>
            /// 属性类型
            /// </summary>
            public swCustomInfoType_e pType { get; set; }

            /// <summary>
            /// 属性值
            /// </summary>
            public string pValue { get; set; }

        }


        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            comboBox1.Enabled = rad2.Checked;

            button1_Click(null, null);

        }

        /// <summary>
        /// 获取属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {

            if (!CheckswModel()) return;

            DGV.Rows.Clear();

            if (rad1.Checked)
            {
                List<SwProperty> Propertys = GetConfigProperty(swModel);
                AddProperty(Propertys);

            }
            if (rad2.Checked)
            {
                List<SwProperty> Propertys = GetConfigProperty(swModel, comboBox1.Text);
                AddProperty(Propertys);
            }

        }

        /// <summary>
        /// 添加属性
        /// </summary>
        /// <param name="Propertys">属性列表</param>
        void AddProperty(List<SwProperty> Propertys)
        {
            foreach (var P in Propertys)
            {
                int RowIndex = DGV.Rows.Add();

                DGV.Rows[RowIndex].Cells["属性名"].Value = P.pName;
                DGV.Rows[RowIndex].Cells["属性类型"].Value = GetPropertyType(P.pType);
                DGV.Rows[RowIndex].Cells["属性值"].Value = P.pValue;

            }
        }


        /// <summary>
        /// 获取属性类型名称
        /// </summary>
        /// <param name="Type">类型</param>
        /// <returns></returns>
        public static string GetPropertyType(swCustomInfoType_e Type)
        {
            switch (Type)
            {
                case swCustomInfoType_e.swCustomInfoUnknown:
                    return "未知";
                case swCustomInfoType_e.swCustomInfoText:
                    return "文字";
                case swCustomInfoType_e.swCustomInfoDate:
                    return "日期";
                case swCustomInfoType_e.swCustomInfoNumber:
                    return "数字";
                case swCustomInfoType_e.swCustomInfoDouble:
                    return "数字";
                case swCustomInfoType_e.swCustomInfoYesOrNo:
                    return "是或否";
                default:
                    return "文字";
            }

        }


        /// <summary>
        /// 获取所有属性名
        /// </summary>
        private void GetPropNames()
        {
            comboBox1.Items.Clear();

            if (swModel == null) return;

            string[] ConfigurationNames = swModel.GetConfigurationNames();

            foreach (var figurationName in ConfigurationNames)
            {
                comboBox1.Items.Add(figurationName);
            }

            comboBox1.SelectedIndex = 0;

        }


        private void Form1_Load(object sender, EventArgs e)
        {
            button1_Click(null, null);

            GetPropNames();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            button1_Click(null, null);
        }
    }
}
